package com.hyf.glidev4.adaoter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.hyf.glidev4.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/12/21.
 */

public class GlideRecyclerAdapter extends RecyclerView.Adapter<GlideRecyclerAdapter.ViewHolder> {

    private Context mContext;

    private String[] urls = {
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2693308818,3869053263&fm=27&gp=0.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846698233&di=10af01948458bb4a469f2c2f88e4f994&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F0d338744ebf81a4cecbd5c1bdd2a6059242da651.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846698233&di=cf95caed3d89351a32cbb7ceecdc14ce&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Ff11f3a292df5e0fe56fe03ec566034a85fdf727d.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846698233&di=b9729c1588c6e2d1ec7705c4b06c9323&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F3ac79f3df8dcd100c146f3cb788b4710b8122f05.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846698233&di=5c2fc26567963b6bd26be1167896b50c&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F43a7d933c895d14318bffdcb79f082025baf0708.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846699968&di=e4c83f160ef03b707ec0044e89f99bc2&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F279759ee3d6d55fb663e14f667224f4a21a4dd62.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846733903&di=2cb4d994b5e2e81d5b5c6abd5472773f&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Faa64034f78f0f736418586e70055b319eac413d9.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846733903&di=4a6d331c2284bb4da610bc3a1a5341a2&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F96dda144ad345982cd3a357a06f431adcaef84a2.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846733902&di=6da060477b6f4d854f72e7e633eaa313&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fac6eddc451da81cb622dc2265866d01608243115.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846793700&di=fca67ae781635584bfe1957370f79c0f&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F64380cd7912397ddd77046eb5382b2b7d1a287d0.jpg",
            "https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D220/sign=97ad951071899e51678e3d1672a6d990/b999a9014c086e06bfc279a608087bf40bd1cbf1.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846793699&di=d151ad1413a90ed88d84e57b2f13c47d&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fbba1cd11728b47102639d61fc9cec3fdfd0323ca.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846793699&di=f2bdec61f891ea3a738d41c1894d42c6&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fd6ca7bcb0a46f21fa0ef77bafc246b600d33ae7b.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513846793699&di=f00c35e49f70539db142bacfd69c1160&imgtype=0&src=http%3A%2F%2Fa.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fe4dde71190ef76c647756fc59716fdfaae5167bd.jpg"
    };

    public GlideRecyclerAdapter(Context context) {
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_recycler_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TextView textView = holder.mTextView;
        ImageView imageView = holder.mImageView;

        imageView.getLayoutParams().height = dip2px(mContext,200);

        textView.setText((position+1)+"");
        RequestOptions options = RequestOptions.placeholderOf(R.mipmap.ic_launcher);
        options.centerCrop();
        Glide.with(mContext)
                .load(urls[position])
                .apply(options)
                .into(imageView);
    }

    @Override
    public int getItemCount() {
        return urls.length;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_recycler_item)
        TextView mTextView;
        @BindView(R.id.iv_recycler_item)
        ImageView mImageView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 将dp转换成系统使用的px
     *
     * @param context 上下文
     * @param dpValue dp值
     * @return 转换后的px值
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
